package com.example.multitouchtest;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

@SuppressLint("NewApi")
public class DrawView extends View implements OnTouchListener {
	Paint leftSidePaint = new Paint();
	Paint rightSidePaint = new Paint();
	int leftWidth = 300;

	public DrawView(Context context) {
		super(context);
		setFocusable(true);
		setFocusableInTouchMode(true);
		this.setOnTouchListener(this);
		leftSidePaint.setColor(Color.BLACK);
		rightSidePaint.setColor(Color.BLACK);

	}

	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawRect(0, 0, 300, canvas.getHeight(), leftSidePaint);
		canvas.drawRect(leftWidth, 0, leftWidth + 300, canvas.getHeight(),
				rightSidePaint);
	}

	public boolean onTouch(View view, MotionEvent event) {
		super.onTouchEvent(event);
		int action = event.getActionMasked();// event.getAction() &
												// MotionEvent.ACTION_MASK;
		int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
		int pointerID = event.getPointerId(pointerIndex);
		int pointerCount = event.getPointerCount();

		int[] touchX = new int[pointerCount];
		int[] touchY = new int[pointerCount];

		for (int i = 0; i < pointerCount; i++) {
			touchX[i] = (int) event.getX(i);
			touchY[i] = (int) event.getY(i);
		}

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			if (touchX[0] < leftWidth) {
				leftSidePaint.setColor(Color.RED);
			}
			if (touchX[0] > leftWidth) {
				rightSidePaint.setColor(Color.RED);
			}
			break;

		case MotionEvent.ACTION_UP:
			if (touchX[0] < leftWidth) {
				leftSidePaint.setColor(Color.BLACK);
			}
			if (touchX[0] > leftWidth) {
				rightSidePaint.setColor(Color.BLACK);
			}
			break;

		case MotionEvent.ACTION_POINTER_DOWN:
			if (touchX[pointerID] >= 0 && touchX[pointerID] < leftWidth) {
				leftSidePaint.setColor(Color.RED);
			}

			if (touchX[pointerID] >= 0 && touchX[pointerID] > leftWidth) {
				rightSidePaint.setColor(Color.RED);
			}
			break;

		case MotionEvent.ACTION_POINTER_UP:
			if (touchX[pointerID] >= 0 && touchX[pointerID] < leftWidth) {
				leftSidePaint.setColor(Color.BLACK);
			}

			if (touchX[pointerID] >= 0 && touchX[pointerID] > leftWidth) {
				rightSidePaint.setColor(Color.BLACK);
			}
			break;
		}
		invalidate();

		return true;
	}

}